<?php
class Controller_Data extends  Controller_Template 
{
        public function __construct($req, $res) 
        {
		$this->template = "smarty:data/daily";
		parent::__construct($req, $res);
	}

        public function action_index ()
        {
            $this->request->redirect('/data/daily');
        }

	public function action_daily($val = NULL)
	{
                $userdetail = account::userinfo(user::current_user_id());
                if($userdetail['account_type'] == 'limit'){
                    $page = $val?$val:1;
                    $limit  = 20;
                    $offset = (max(1, $page) - 1) * $limit;
                    $query  = null;
                    
                    $arr = Data::stats_limit($query, $offset, $limit);
//                    var_dump($arr);exit;
                    $this->template->set('daily', $arr['list']);
                    $this->template->set('pagination', $this->pagination($arr['count'], 20, 'route', 'param1', 'pagination/simple'));
                    $userinfo = account::userinfo(user::current_user_id());
                    $this->template->set('account_type', $userinfo['account_type']);
                }else{
                    $start_date = $_GET['start_date'];
                    $end_date   = $_GET['end_date'];
                    $machine    = $_GET['machine'];

                    if($start_date==""||$end_date=="")
                    {
                        $start_date = date("Y/m/d", time() - 86400 * 7);
                        $end_date   = date("Y/m/d", time());
                    }
                    $uid = $val;
                    if($uid == NULL){
                        $uid = user::current_user_id();
                    }
                    $accounts = Account::children($uid);
                    $userdetail = account::userinfo(user::current_user_id()); 
//                    var_dump($accounts);
                    $daily = Data::stats($uid, NULL,$start_date, $end_date, $machine);
                    $this->template->set('daily', $daily);
                    $this->template->set('start_date', $start_date);
                    $this->template->set('end_date', $end_date);
                    $this->template->set('accounts', $accounts);
                    $this->template->set('machine_selected', $machine);
                    $this->template->set('userdetail', $userdetail);
                }
                $this->template->set('account_type', $userdetail['account_type']);
	}

        // 
        // 上传SD卡日志
        //
        public function action_upload()
        {
            if (empty($_FILES))
            {
                $this->show_upload();
                return;
            }

		    $keys = array_keys($_FILES);
		    if (empty($keys))
                throw new Exception_Warn('没有上传日志文件');

		    $user = user::current_user();

	    	try {
                $name = $_FILES[$keys[0]]['name'];
                list($url, $location) = Media::save_upload_file($keys[0]);
                data::store_sd_logs ($user->get_user_id(), $name, $url, $location);
            }
            catch (Exception_Warn $e)
            {
                $this->show_upload();
                return;
            }

            $this->template->set('upload_success', TRUE);
            $this->template->set('records', Data::sdlogs(user::current_user_id()));
            $this->template->set_filename('smarty:data/upload');
        }

        public function action_day ($day,$uid = NULL)
        {
                if($uid == NULL){
                    $uid = user::current_user_id();
                }
                $records = Data::daily($day, $uid);
                $userinfo = account::userinfo(user::current_user_id());
                $this->template->set('records', $records);
                $this->template->set('account_type', $userinfo['account_type']);
                $this->template->set_filename('smarty:data/day');
        }

        private function show_upload()
        {
            $this->template->set('records', Data::sdlogs(user::current_user_id()));
            $this->template->set_filename('smarty:data/upload');
        }
        
        public function action_apps($imei, $day, $uid = NULL)
        {
            if($uid == NULL){
                $uid = user::current_user_id();
            }
            $apps = Data::apps($imei, $day, $uid);
            $this->template->set('apps', $apps);
            $this->template->set_filename('smarty:data/apps');
        }
}
